<?php
class Admin_Form_ValidateManufacturers extends Zendvn_Validate_Form{
	public function __construct($arrParam = null, $options = null){
		define('NAME_ID',$arrParam['id']);
		$config 		= new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
													 	  'fileConfig'=>'validates/manufacturers.ini'));
		
		//================================Validate Name================================
		$name  = $config->getConfig('name');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['name'])){
			$message = $validator->getMessages();
			$this->_arrError['name'] = current($message);
			$arrParam['name'] = '';
		}
		
		//================================Validate Ordering================================
		$name  = $config->getConfig('ordering');
		$validator  =  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['ordering'])){
			$message = $validator->getMessages();
			$this->_arrError['ordering'] = current($message);
			$arrParam['ordering'] = '';
		}
		
		/*=======================================================
		 * Start - validate picture
		 *=======================================================*/
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];

		if(!empty($fileName)){
			$options = array('gif','png','jpg','jpeg');
			$upload->addValidator('Extension',true,$options,'picture')
			 	   ->addValidator('Size',true,array('min'=>'5b','max'=>'10000kb'),'picture');
			if(!$upload->isValid('picture')){
				$message = $upload->getMessages();
				$this->_arrError['picture'] = current($message);
				$arrParam['picture'] = '';
			}
		}else{
			if($arrParam["action"] == 'edit'){
				$arrParam['picture'] = $arrParam['current_picture'];
			}
		}

		/*=======================================================
		 * Start - return correct data array
		 *=======================================================*/
		$this->_arrData = $arrParam;
	}

	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($arrFile,$this->_arrData);
			}
		}
		return $this->_arrData;
	}

	//return true|false
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0){
			$flag = true;
		}
		return $flag;
	}

	public function getMessageErrors(){
		return $this->_arrError;
	}

	public function upload(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'],
													 	  'fileConfig'=>'manufacturers.ini'));
		$categoryConfig = $config->getConfig('item');
		/*=======================================================
		 * Start - upload picture
		 *=======================================================*/

		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('picture');
		$fileName = $fileInfo['picture']['name'];

		if(!empty($fileName)){		
			$options = array('custom_rename'=> true,'uploadDir'=> $categoryConfig['uploadDir']);			
			$fileName = $upload->upload($categoryConfig['uploadDir'],'picture',$options);
			$arrFile['picture'] = $fileName;
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($categoryConfig['uploadDir'], $this->_arrData['current_picture']);
			}
		}
		return $arrFile;
	}
}















